package com.atguigu.bytebit;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.junit.Test;
import sun.nio.cs.ext.GBK;

import javax.crypto.Cipher;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;

public class Bytebit {
    @Test
    public void test() throws Exception {
        /*String s = desDeleteSecret("Dg7u87ZPA02Ql1Uh4qfpTDShkFvi67Jl", "涛涛爱红", "DES", "DES");
        System.out.println(s);*/
        String des = dncryptDES("8i9NPk1NmJj/zdzpZJKoFArs4gNcAvYg", "涛涛爱红", "DES/CBC/PKCS5Padding","DES");
        System.out.println("des = " + des);
    }
    @Test
    public void bytebitTest(){
    String a = "宋磊";
        byte[] bytes = a.getBytes();
        for (byte aByte : bytes) {
            System.out.println("aByte = " + aByte);
            String aBit = Integer.toBinaryString(aByte);
            System.out.println("aBit = " + aBit);
        }
    }
    @Test
    public void desTest() throws Exception {
        String message = "有内鬼终止交易";
        String key = "涛涛爱红";
        //算法
        String algorithm = "DES";
        String transformation = "DES";
        //表示使用什么类型加密
        Cipher cip = Cipher.getInstance(transformation);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("GBK"), algorithm);
        //对加密进行初始化
        cip.init(Cipher.ENCRYPT_MODE,secretKeySpec);
        byte[] bytes = cip.doFinal(message.getBytes());
        System.out.println("bytes = " + new String(bytes));
        String encode = Base64.encode(bytes);
        System.out.println("encode = " + encode);
    }
    public  String desAddSecret(String message,String key,String algorithm ,String transformation) throws Exception {
        Cipher cip = Cipher.getInstance(transformation);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("GBK"), algorithm);
        cip.init(Cipher.ENCRYPT_MODE,secretKeySpec);
        byte[] bytes = cip.doFinal(message.getBytes());
        String encode = Base64.encode(bytes);
        return encode;
    }
    public String desDeleteSecret(String message,String key,String algorithm ,String transformation) throws Exception{
        Cipher cip = Cipher.getInstance(transformation);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("GBK"), algorithm);
        cip.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] bytes = cip.doFinal(Base64.decode(message));
        return new String(bytes);
    }
    @Test
    public void desCBC() throws Exception {
        String input = "有内鬼终止交易";
        String key = "涛涛爱红";
        String transformation = "DES/CBC/PKCS5Padding";
        String algorithm = "DES";
        String encryptDES = encryptDES(input,key,transformation,algorithm);
        System.out.println("encryptDES = " + encryptDES);
    }
    public  String encryptDES(String input,String key,String transformation,String algorithm) throws Exception{
        Cipher cipher = Cipher.getInstance(transformation);
        SecretKeySpec sks = new SecretKeySpec(key.getBytes("GBK"), algorithm);
        IvParameterSpec ivp = new IvParameterSpec("涛涛爱红".getBytes("GBK"));
        cipher.init(Cipher.ENCRYPT_MODE,sks,ivp);
        byte[] bytes = cipher.doFinal(input.getBytes());
        String encode = Base64.encode(bytes);
        return encode;
    }
    public  String dncryptDES(String input,String key,String transformation,String algorithm) throws Exception{
        Cipher cipher = Cipher.getInstance(transformation);
        SecretKeySpec sks = new SecretKeySpec(key.getBytes("GBK"), algorithm);
        IvParameterSpec ivp = new IvParameterSpec(key.getBytes("GBK"));
        cipher.init(Cipher.DECRYPT_MODE,sks,ivp);
        byte[] bytes = cipher.doFinal(Base64.decode(input));
        return new String(bytes);
    }
    @Test
    public void rsaDemo() throws Exception {
        String input = "钟叔";
        String algorithm = "RSA";
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
        KeyPair keyPair = kpg.generateKeyPair();
        //获取私钥
        PrivateKey aPrivate = keyPair.getPrivate();
        //获取公钥
        PublicKey aPublic = keyPair.getPublic();
        byte[] aPrivateEncoded = aPrivate.getEncoded();
        byte[] aPublicEncoded = aPublic.getEncoded();
        String encodePrivate = Base64.encode(aPrivateEncoded);
        String encodePublic = Base64.encode(aPublicEncoded);
        System.out.println("encodePrivate = " + encodePrivate);
        System.out.println("encodePublic = " + encodePublic);
        /*私钥加密公钥解密*/
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE,aPrivate);
        byte[] bytes = cipher.doFinal(input.getBytes());
        System.out.println(Base64.encode(bytes));
        //用公钥进行解密
        cipher.init(Cipher.DECRYPT_MODE,aPublic);
        byte[] bytes1 = cipher.doFinal(bytes);
        System.out.println("bytes1 = " + new String(bytes1));
    }
}
